package com.msb.crm.interceptor;

import com.msb.crm.exceptions.NoLoginException;
import com.msb.crm.service.UserService;
import com.msb.crm.utils.LoginUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NoLoginInterceptor implements HandlerInterceptor {


    @Resource
    private UserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取Cookie中的用户Id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        //判断用户Id是否不为空，且数据库中是否存在对应的用户记录
        if ( null == userId || null == userService.selectByPrimaryKey(userId)){
            //判断未登录异常
            throw new NoLoginException();
            //888888
        }
        return true;
    }
}
